package com.zjitc.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.RespBean;
import com.zjitc.serviceedu.entity.EduEssay;
import com.zjitc.serviceedu.entity.vo.EssayAvatarVo;
import com.zjitc.serviceedu.entity.vo.EssayVo;
import com.zjitc.serviceedu.service.EduEssayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-10-14
 */
@RestController
@RequestMapping("/serviceedu/edu-essay")
@Api(tags = "文章模块")
public class EduEssayController {
    @Autowired
    private EduEssayService eduEssayService;

    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
    @PostMapping("addEssay")
    @ApiOperation("添加文章")
    public RespBean addEssay(EduEssay eduEssay) {
        boolean result = eduEssayService.save(eduEssay);
        if (!result) {
            throw new AuthException("添加失败");
        }

        return RespBean.success().msg("添加成功");
    }

    @ApiOperation("修改详情查询")
    @GetMapping("getById/{id}")
    public RespBean getById(@PathVariable("id") Long id) {
        EduEssay essay = eduEssayService.getById(id);
        if (essay == null) {
            throw new AuthException("查询失败");
        }

        return RespBean.success().data("data", essay).msg("查询成功");
    }
    @ApiOperation("文章查询讲师头像")
    @GetMapping("getTeacherAvatar")
    public RespBean getTeacherAvatar(EssayAvatarVo EssayAvatarVo) {
        List<EssayAvatarVo> result  = eduEssayService.getAvatar(EssayAvatarVo);
        if (result == null) {
            throw new AuthException("查询失败");
        }

        return RespBean.success().data("data", result).msg("查询成功");
    }


    @ApiOperation("文章id查询讲师头像")
    @GetMapping("getTeacherAvatar/{teacherId}")
    public RespBean getTeacherId( @PathVariable("teacherId") Long teacherId) {
        EssayAvatarVo result  = eduEssayService.getTeacherId(teacherId);
        if (result == null) {
            throw new AuthException("查询失败");
        }

        return RespBean.success().data("data", result).msg("查询成功");
    }

    @ApiOperation("修改文章")
    @PutMapping("updateEssay")
    public RespBean updateEssay(EduEssay eduEssay) {
        boolean result = eduEssayService.updateById(eduEssay);
        if (!result) {
            throw new AuthException("修改失败");
        }

        return RespBean.success().msg("修改成功");
    }

    @ApiOperation("删除文章")
    @DeleteMapping("delEssay/{id}")
    public RespBean delEssay(@PathVariable("id") Long id) {
        boolean result = eduEssayService.removeById(id);
        if (!result) {
            throw new AuthException("删除失败");
        }

        return RespBean.success().msg("删除成功");
    }

    @ApiOperation("更新文章浏览量")
    @PostMapping("/updateBrowse/{id}")
    public RespBean updateBrowse(@PathVariable Long id) {
        // 调用 service 层来更新 viewCount
        boolean success = eduEssayService.updateBrowse(id);
        if (success) {
            return RespBean.success().msg("浏览量更新成功");
        } else {
            return RespBean.error(201, "更新失败");
        }
    }
    @ApiOperation("添加文章点赞数")
    @PostMapping("/updateThumbUp/{id}")
    public RespBean updateThumbUp(@PathVariable Long id) {
        // 调用 service 层来更新 viewCount
        boolean success = eduEssayService.updateThumbUp(id);
        if (success) {
            return RespBean.success().msg("点赞数更新成功");
        } else {
            return RespBean.error(201, "更新失败");
        }
    }
    @ApiOperation("条件查询")
    @GetMapping("getPageBatchs")
    public RespBean getPageBatchs(EssayVo essayVo) {

        Page<EduEssay> page = new Page<>(essayVo.getPage(), essayVo.getSize());

        QueryWrapper<EduEssay> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(essayVo.getAuthor())) {
            queryWrapper.like("author", essayVo.getAuthor());
        }
        if (!StringUtils.isEmpty(essayVo.getTitle())) {
            queryWrapper.ge("title", essayVo.getTitle());
        }
        if (!StringUtils.isEmpty(essayVo.getType())) {
            queryWrapper.ge("Type", essayVo.getType());
        }
        queryWrapper.orderByAsc("id");
        eduEssayService.page(page, queryWrapper);

        if (page == null) {
            throw new AuthException("查询失败");
        }
        List<EduEssay> list = page.getRecords();
        Long total = page.getTotal();

        return RespBean.success().data("list", list).data("total", total).msg("查询成功");

    }

}

